package com.dhecp.project.api.controller;

import com.dhecp.common.core.controller.BaseController;
import com.dhecp.project.api.domain.ApiResponse;
import com.dhecp.project.api.service.ApiLogService;
import com.dhecp.project.api.service.ApiProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName ABBApiController
 * @Description TODO
 * @Author alula
 * @Date 2020-04-10 10:20
 */
@Controller
@RequestMapping("/api/product")
public class ApiProductController extends BaseController {

    //数据采集系统名称
    private static final String DAS_SYSTEM_NAME = "das";

    @Autowired
    private ApiProductService apiProductService;

    @Autowired
    private ApiLogService apiLogService;


    /**
     产品保存
     */
    @RequestMapping("/save")
    @ResponseBody
    public ApiResponse saveProduct(@RequestParam(required = true) String appId,
                                     @RequestParam(required = true) String sign,
                                     @RequestParam(required = true) long timestamp,
                                     @RequestParam(required = true) String body)
    {
        ApiResponse apiResponse = new ApiResponse();
        int insertLogId = 0;
        String type = "SaveProduct";
        try {
            insertLogId = apiLogService.addApiLog(DAS_SYSTEM_NAME,type,"appId："+appId +"，sign："+sign +"，timestamp：" +timestamp + "，body：" +body,"",0, "");
            apiResponse = apiProductService.saveProduct(insertLogId, DAS_SYSTEM_NAME, type, appId, sign, timestamp, body);
            if (apiResponse.getCode() == 500){
                apiLogService.updateApiLog(insertLogId, DAS_SYSTEM_NAME, type, "appId："+appId +"，sign："+sign +"，timestamp：" +timestamp + "，body：" +body, "ECP_API接口处理产品采集系统传送产品数据保存出错，错误为："+apiResponse.getMessage(), apiResponse.getCode(), apiResponse.getMessage());
            }
        }
        catch (Exception ex) {
            apiResponse.setCode(500);
            apiResponse.setMessage("ECP_API接口处理产品采集系统传送产品数据保存出错，错误为" + ex.getMessage());
        }
        return apiResponse;
    }

}
